package com.example.demo.vo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author shenb
 * @date 2022-06-28 9:52
 */
public class Result<T> {
    public static final int SUCCESS_CODE = 0;
    public static final int DEFAULT_ERROR_CODE = 1;

    private int code;
    private String message;
    private T data;

    public Result() {
    }

    public Result(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static Result<Void> success() {
        return new Result<>(SUCCESS_CODE, null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(SUCCESS_CODE, null, data);
    }

    public static <T> Result<T> failure(T data) {
        return new Result<>(DEFAULT_ERROR_CODE, null, data);
    }

    public static <T> Result<T> failure(String message, T data) {
        return new Result<>(DEFAULT_ERROR_CODE, message, data);
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Map<String, Object> toMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        map.put("message", message);
        map.put("data", data);
        return map;
    }
}
